package com.yiyang.cloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yiyang.cloud.feign.UserFeignClient;
import com.yiyang.cloud.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping("/users/{id}")
    public User findByUserId(@PathVariable("id") Long id) {
        return restTemplate.getForObject("http://user-center/users/" + id, User.class);
    }

    @GetMapping("/load-balance/user-instance")
    public ServiceInstance loadBalanceInstance() {
        ServiceInstance choose = loadBalancerClient.choose("user-center");
        System.out.println("发现服务：" + choose.getServiceId() + ", IP 地址：" + choose.getUri() + ", 端口号：" + choose.getPort());
        return choose;
    }

}
